package com.qs.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qs.constant.StatusConstant;
import com.qs.constant.UserGoodsLogConstant;
import com.qs.mapper.*;
import com.qs.pojo.dto.CartDto;
import com.qs.pojo.dto.UserGoodsLogDto;
import com.qs.pojo.dto.UserGoodsLogOrderDto;
import com.qs.pojo.entity.*;
import com.qs.pojo.enums.UserGoodsStatus;
import com.qs.service.IUserGoodsLogService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 用户购买商品历史表 服务实现类
 * </p>
 *
 * @author author
 * @since 2023-12-09
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class UserGoodsLogServiceImpl extends ServiceImpl<UserGoodsLogMapper, UserGoodsLog> implements IUserGoodsLogService {

    private final GoodsMapper goodsMapper;

    private final OrderItemMapper orderItemMapper;

    private final AddressMapper addressMapper;

    private final OrderInfoMapper orderInfoMapper;

    @Override
    public List<UserGoodsLogDto> getWaitDelivery(Integer userId) {
        //根据用户id查询购物车
        QueryWrapper<UserGoodsLog> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        wrapper.eq("status", UserGoodsStatus.WAIT_COMMENT);
        wrapper.orderByDesc("update_time");
        List<UserGoodsLog> userGoodsLogs = baseMapper.selectList(wrapper);
        if(!CollectionUtils.isEmpty(userGoodsLogs)){
            List<UserGoodsLogDto> list = new ArrayList<UserGoodsLogDto>();
            userGoodsLogs.stream().forEach(item -> {
                QueryWrapper<OrderItem> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("goods_id",item.getGoodsId());
                queryWrapper.eq("order_id",item.getOrderId());
                List<OrderItem> orderItems = orderItemMapper.selectList(queryWrapper);
                if(!CollectionUtils.isEmpty(orderItems)){
                    orderItems.stream().forEach(orderItem -> {
                        Goods goods = goodsMapper.selectById(item.getGoodsId());
                        UserGoodsLogDto userGoodsLogDto = new UserGoodsLogDto();
                        userGoodsLogDto.setGoodsId(item.getGoodsId());
                        userGoodsLogDto.setOrderId(item.getOrderId());
                        userGoodsLogDto.setUserId(userId);
                        userGoodsLogDto.setPrice(goods.getPrice());
                        userGoodsLogDto.setImage(goods.getImage());
                        userGoodsLogDto.setName(goods.getName());
                        userGoodsLogDto.setNumber(orderItem.getNumber());
                        userGoodsLogDto.setSubTotalPrice(orderItem.getNumber() * goods.getPrice());
                        userGoodsLogDto.setStatus(UserGoodsStatus.WAIT_COMMENT.getComment());
                        list.add(userGoodsLogDto);
                    });
                }
            });
            return list;
        }
        return null;
    }

    @Override
    public List<UserGoodsLogDto> getWaitTake(Integer userId) {
        //根据用户id查询购物车
        QueryWrapper<UserGoodsLog> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        wrapper.eq("status", UserGoodsStatus.HAVE_COMMENT);
        wrapper.orderByDesc("update_time");
        List<UserGoodsLog> userGoodsLogs = baseMapper.selectList(wrapper);
        if(!CollectionUtils.isEmpty(userGoodsLogs)){
            List<UserGoodsLogDto> list = new ArrayList<UserGoodsLogDto>();
            userGoodsLogs.stream().forEach(item -> {
                QueryWrapper<OrderItem> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("goods_id",item.getGoodsId());
                queryWrapper.eq("order_id",item.getOrderId());
                List<OrderItem> orderItems = orderItemMapper.selectList(queryWrapper);
                if(!CollectionUtils.isEmpty(orderItems)){
                    orderItems.stream().forEach(orderItem -> {
                        Goods goods = goodsMapper.selectById(item.getGoodsId());
                        UserGoodsLogDto userGoodsLogDto = new UserGoodsLogDto();
                        userGoodsLogDto.setGoodsId(item.getGoodsId());
                        userGoodsLogDto.setOrderId(item.getOrderId());
                        userGoodsLogDto.setUserId(userId);
                        userGoodsLogDto.setPrice(goods.getPrice());
                        userGoodsLogDto.setImage(goods.getImage());
                        userGoodsLogDto.setName(goods.getName());
                        userGoodsLogDto.setNumber(orderItem.getNumber());
                        userGoodsLogDto.setSubTotalPrice(orderItem.getNumber() * goods.getPrice());
                        userGoodsLogDto.setStatus(UserGoodsStatus.HAVE_COMMENT.getComment());
                        list.add(userGoodsLogDto);
                    });
                }
            });
            return list;
        }
        return null;
    }

    @Override
    public List<UserGoodsLogDto> getWaitComment(Integer userId) {
        //根据用户id查询购物车
        QueryWrapper<UserGoodsLog> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        wrapper.eq("is_comment", UserGoodsLogConstant.NOT_COMMENT);
        wrapper.orderByDesc("update_time");
        List<UserGoodsLog> userGoodsLogs = baseMapper.selectList(wrapper);
        if(!CollectionUtils.isEmpty(userGoodsLogs)){
            List<UserGoodsLogDto> list = new ArrayList<UserGoodsLogDto>();
            userGoodsLogs.stream().forEach(item -> {
                QueryWrapper<OrderItem> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("goods_id",item.getGoodsId());
                queryWrapper.eq("order_id",item.getOrderId());
                List<OrderItem> orderItems = orderItemMapper.selectList(queryWrapper);
                if(!CollectionUtils.isEmpty(orderItems)){
                    orderItems.stream().forEach(orderItem -> {
                        Goods goods = goodsMapper.selectById(item.getGoodsId());
                        UserGoodsLogDto userGoodsLogDto = new UserGoodsLogDto();
                        userGoodsLogDto.setGoodsId(item.getGoodsId());
                        userGoodsLogDto.setOrderId(item.getOrderId());
                        userGoodsLogDto.setUserId(userId);
                        userGoodsLogDto.setPrice(goods.getPrice());
                        userGoodsLogDto.setImage(goods.getImage());
                        userGoodsLogDto.setName(goods.getName());
                        userGoodsLogDto.setNumber(orderItem.getNumber());
                        userGoodsLogDto.setSubTotalPrice(orderItem.getNumber() * goods.getPrice());
                        userGoodsLogDto.setStatus(UserGoodsLogConstant.NOT_COMMENT_ED);
                        list.add(userGoodsLogDto);
                    });
                }
            });
            return list;
        }
        return null;
    }

    @Override
    public List<UserGoodsLogDto> getEndGoods(Integer userId) {
        //根据用户id查询购物车
        QueryWrapper<UserGoodsLog> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        wrapper.eq("status", UserGoodsStatus.END);
        wrapper.eq("is_comment", UserGoodsLogConstant.DO_COMMENT);
        wrapper.orderByDesc("update_time");
        List<UserGoodsLog> userGoodsLogs = baseMapper.selectList(wrapper);
        if(!CollectionUtils.isEmpty(userGoodsLogs)){
            List<UserGoodsLogDto> list = new ArrayList<UserGoodsLogDto>();
            userGoodsLogs.stream().forEach(item -> {
                QueryWrapper<OrderItem> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("goods_id",item.getGoodsId());
                queryWrapper.eq("order_id",item.getOrderId());
                OrderItem orderItem = orderItemMapper.selectOne(queryWrapper);
                Goods goods = goodsMapper.selectById(item.getGoodsId());
                UserGoodsLogDto userGoodsLogDto = new UserGoodsLogDto();
                userGoodsLogDto.setGoodsId(item.getGoodsId());
                userGoodsLogDto.setUserId(userId);
                userGoodsLogDto.setOrderId(item.getOrderId());
                userGoodsLogDto.setPrice(goods.getPrice());
                userGoodsLogDto.setImage(goods.getImage());
                userGoodsLogDto.setName(goods.getName());
                userGoodsLogDto.setNumber(orderItem.getNumber());
                userGoodsLogDto.setSubTotalPrice(orderItem.getNumber() * goods.getPrice());
                userGoodsLogDto.setStatus(UserGoodsLogConstant.END_GOODS);
                list.add(userGoodsLogDto);
            });
            return list;
        }
        return null;
    }

    @Override
    public void confirmTakeGoods(Integer goodsId,String orderId) {
        QueryWrapper<UserGoodsLog> wrapper = new QueryWrapper<>();
        wrapper.eq("goods_id",goodsId);
        wrapper.eq("order_id",orderId);
        UserGoodsLog userGoodsLog = baseMapper.selectOne(wrapper);
        userGoodsLog.setStatus(UserGoodsStatus.END);
        baseMapper.updateById(userGoodsLog);
    }

    @Override
    public Map<String, Object> getWaitOrders(Integer pageNo, Integer pageSize, Integer storeId) {
        Page<UserGoodsLog> pageInfo = new Page<>(pageNo, pageSize);
        QueryWrapper<UserGoodsLog> wrapper = new QueryWrapper<>();
        wrapper.eq("store_id", storeId);
        wrapper.eq("status", UserGoodsStatus.WAIT_COMMENT);
        wrapper.orderByDesc("update_time");
        Page<UserGoodsLog> userGoodsLogPage = baseMapper.selectPage(pageInfo, wrapper);
        if(null != userGoodsLogPage){
            List<UserGoodsLog> userGoodsLogList = userGoodsLogPage.getRecords();
            List<UserGoodsLogOrderDto> list = new ArrayList<>();
            if(!CollectionUtils.isEmpty(userGoodsLogList)){
                userGoodsLogList.stream().forEach(item -> {
                    QueryWrapper<OrderItem> queryWrapper = new QueryWrapper<>();
                    queryWrapper.eq("goods_id",item.getGoodsId());
                    queryWrapper.eq("order_id",item.getOrderId());
                    OrderItem orderItem = orderItemMapper.selectOne(queryWrapper);

                    QueryWrapper<OrderInfo> queryOrderInfo = new QueryWrapper<>();
                    queryOrderInfo.eq("order_id",item.getOrderId());
                    OrderInfo orderInfo = orderInfoMapper.selectOne(queryOrderInfo);

                    Address address = addressMapper.selectById(orderInfo.getAddressId());

                    Goods goods = goodsMapper.selectById(item.getGoodsId());

                    UserGoodsLogOrderDto userGoodsLogOrderDto = new UserGoodsLogOrderDto();
                    userGoodsLogOrderDto.setId(item.getId());
                    userGoodsLogOrderDto.setName(goods.getName());
                    userGoodsLogOrderDto.setPhone(address.getPhone());
                    userGoodsLogOrderDto.setImage(goods.getImage());
                    userGoodsLogOrderDto.setAddress(address.getRegion() + address.getDetail());
                    userGoodsLogOrderDto.setNumber(orderItem.getNumber());
                    list.add(userGoodsLogOrderDto);
                });
                Map<String,Object> map = new HashMap<>();
                map.put("data",list);
                map.put("total",userGoodsLogPage.getTotal());
                return map;
            }
            return null;
        }
        return null;
    }

    @Override
    public void toDoDelivery(Integer id) {
        UserGoodsLog userGoodsLog = baseMapper.selectById(id);
        userGoodsLog.setStatus(UserGoodsStatus.HAVE_COMMENT);
        baseMapper.updateById(userGoodsLog);
    }

    @Override
    public Map<String, Object> getTakeGoods(Integer pageNo, Integer pageSize, Integer storeId) {
        Page<UserGoodsLog> pageInfo = new Page<>(pageNo, pageSize);
        QueryWrapper<UserGoodsLog> wrapper = new QueryWrapper<>();
        wrapper.eq("store_id", storeId);
        wrapper.eq("status", UserGoodsStatus.HAVE_COMMENT);
        wrapper.orderByDesc("update_time");
        Page<UserGoodsLog> userGoodsLogPage = baseMapper.selectPage(pageInfo, wrapper);
        if(null != userGoodsLogPage){
            List<UserGoodsLog> userGoodsLogList = userGoodsLogPage.getRecords();
            List<UserGoodsLogOrderDto> list = new ArrayList<>();
            if(!CollectionUtils.isEmpty(userGoodsLogList)){
                userGoodsLogList.stream().forEach(item -> {
                    QueryWrapper<OrderItem> queryWrapper = new QueryWrapper<>();
                    queryWrapper.eq("goods_id",item.getGoodsId());
                    queryWrapper.eq("order_id",item.getOrderId());
                    OrderItem orderItem = orderItemMapper.selectOne(queryWrapper);

                    QueryWrapper<OrderInfo> queryOrderInfo = new QueryWrapper<>();
                    queryOrderInfo.eq("order_id",item.getOrderId());
                    OrderInfo orderInfo = orderInfoMapper.selectOne(queryOrderInfo);

                    Address address = addressMapper.selectById(orderInfo.getAddressId());

                    Goods goods = goodsMapper.selectById(item.getGoodsId());

                    UserGoodsLogOrderDto userGoodsLogOrderDto = new UserGoodsLogOrderDto();
                    userGoodsLogOrderDto.setId(item.getId());
                    userGoodsLogOrderDto.setName(goods.getName());
                    userGoodsLogOrderDto.setPhone(address.getPhone());
                    userGoodsLogOrderDto.setImage(goods.getImage());
                    userGoodsLogOrderDto.setAddress(address.getRegion() + address.getDetail());
                    userGoodsLogOrderDto.setNumber(orderItem.getNumber());
                    list.add(userGoodsLogOrderDto);
                });
                Map<String,Object> map = new HashMap<>();
                map.put("data",list);
                map.put("total",userGoodsLogPage.getTotal());
                return map;
            }
            return null;
        }
        return null;
    }

    @Override
    public Map<String, Object> getReceivedGoods(Integer pageNo, Integer pageSize, Integer storeId) {
        Page<UserGoodsLog> pageInfo = new Page<>(pageNo, pageSize);
        QueryWrapper<UserGoodsLog> wrapper = new QueryWrapper<>();
        wrapper.eq("store_id", storeId);
        wrapper.eq("status", UserGoodsStatus.END);
        wrapper.orderByDesc("update_time");
        Page<UserGoodsLog> userGoodsLogPage = baseMapper.selectPage(pageInfo, wrapper);
        if(null != userGoodsLogPage){
            List<UserGoodsLog> userGoodsLogList = userGoodsLogPage.getRecords();
            List<UserGoodsLogOrderDto> list = new ArrayList<>();
            if(!CollectionUtils.isEmpty(userGoodsLogList)){
                userGoodsLogList.stream().forEach(item -> {
                    QueryWrapper<OrderItem> queryWrapper = new QueryWrapper<>();
                    queryWrapper.eq("goods_id",item.getGoodsId());
                    queryWrapper.eq("order_id",item.getOrderId());
                    OrderItem orderItem = orderItemMapper.selectOne(queryWrapper);

                    QueryWrapper<OrderInfo> queryOrderInfo = new QueryWrapper<>();
                    queryOrderInfo.eq("order_id",item.getOrderId());
                    OrderInfo orderInfo = orderInfoMapper.selectOne(queryOrderInfo);

                    Address address = addressMapper.selectById(orderInfo.getAddressId());

                    Goods goods = goodsMapper.selectById(item.getGoodsId());

                    UserGoodsLogOrderDto userGoodsLogOrderDto = new UserGoodsLogOrderDto();
                    userGoodsLogOrderDto.setId(item.getId());
                    userGoodsLogOrderDto.setName(goods.getName());
                    userGoodsLogOrderDto.setPhone(address.getPhone());
                    userGoodsLogOrderDto.setImage(goods.getImage());
                    userGoodsLogOrderDto.setAddress(address.getRegion() + address.getDetail());
                    userGoodsLogOrderDto.setNumber(orderItem.getNumber());
                    list.add(userGoodsLogOrderDto);
                });
                Map<String,Object> map = new HashMap<>();
                map.put("data",list);
                map.put("total",userGoodsLogPage.getTotal());
                return map;
            }
            return null;
        }
        return null;
    }
}
